package com.rongshu.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.submitArticle.bean.LabelBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhengweis on 2018/12/12.
 */

public class ArticleLabelView extends LinearLayout {

    @BindView(R.id.article_label_1)
    TextView articleLabel1;
    @BindView(R.id.dividing_line_1)
    View dividingLine1;
    @BindView(R.id.article_label_2)
    TextView articleLabel2;
    @BindView(R.id.dividing_line_2)
    View dividingLine2;
    @BindView(R.id.article_label_3)
    TextView articleLabel3;
    @BindView(R.id.dividing_line_3)
    View dividingLine3;
    @BindView(R.id.article_label_4)
    TextView articleLabel4;
    private List<LabelBean> labels;

    public ArticleLabelView(Context context) {
        super(context);
        initView();
    }

    public ArticleLabelView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public ArticleLabelView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void bindDatas(List<LabelBean> labels) {
        this.labels = labels;
        updateView();

    }

    private void initView() {
        setOrientation(LinearLayout.HORIZONTAL);
        View view = View.inflate(getContext(), R.layout.layout_article_label_item, this);
        ButterKnife.bind(this, view);

    }

    private void updateView() {
        if (labels != null) {
            switch (labels.size()) {
                case 1:
                    articleLabel1.setVisibility(VISIBLE);
                    articleLabel2.setVisibility(GONE);
                    articleLabel3.setVisibility(GONE);
                    articleLabel4.setVisibility(GONE);
                    dividingLine1.setVisibility(GONE);
                    dividingLine2.setVisibility(GONE);
                    dividingLine3.setVisibility(GONE);
                    break;
                case 2:
                    articleLabel1.setVisibility(VISIBLE);
                    articleLabel2.setVisibility(VISIBLE);
                    articleLabel3.setVisibility(GONE);
                    articleLabel4.setVisibility(GONE);
                    dividingLine1.setVisibility(VISIBLE);
                    dividingLine2.setVisibility(GONE);
                    dividingLine3.setVisibility(GONE);
                    break;
                case 3:
                    articleLabel1.setVisibility(VISIBLE);
                    articleLabel2.setVisibility(VISIBLE);
                    articleLabel3.setVisibility(VISIBLE);
                    articleLabel4.setVisibility(GONE);
                    dividingLine1.setVisibility(VISIBLE);
                    dividingLine2.setVisibility(VISIBLE);
                    dividingLine3.setVisibility(GONE);
                    break;
                case 4:
                    articleLabel1.setVisibility(VISIBLE);
                    articleLabel2.setVisibility(VISIBLE);
                    articleLabel3.setVisibility(VISIBLE);
                    articleLabel4.setVisibility(VISIBLE);
                    dividingLine1.setVisibility(VISIBLE);
                    dividingLine2.setVisibility(VISIBLE);
                    dividingLine3.setVisibility(VISIBLE);
                    break;
            }
            for (int index = 0; index < labels.size(); index++) {
                switch (index) {
                    case 0:
                        articleLabel1.setText(labels.get(index).getTitle());
                        break;
                    case 1:
                        articleLabel2.setText(labels.get(index).getTitle());
                        break;
                    case 2:
                        articleLabel3.setText(labels.get(index).getTitle());
                        break;
                    case 3:
                        articleLabel4.setText(labels.get(index).getTitle());
                        break;
                }
            }
        }
    }
}
